package com.molyfun.parents.activity.store.view;

import android.content.Context;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.molyfun.parents.R;
import com.molyfun.parents.rest.bean.ExpenseRecordListResponse;
import com.molyfun.parents.utils.StringUtils;
import com.molyfun.parents.utils.TimeUtils;

import butterknife.ButterKnife;
import butterknife.InjectView;

/**
 * Created by william(liuchao.hit@gmail.com).
 * Date:10/18/16.
 */
public class ExpenseRecordCellView extends LinearLayout implements View.OnClickListener{

    protected LayoutInflater layoutInflater;

    @InjectView(R.id.product_name)
    TextView mProductName;
    @InjectView(R.id.student_remarkname)
    TextView mStudentRemarkName;
    @InjectView(R.id.organization_name)
    TextView mOrganizationName;
    @InjectView(R.id.record_time_interval)
    TextView mRecordTimeInterval;
    @InjectView(R.id.product_content_name)
    TextView mProductContentName;
    @InjectView(R.id.product_content_count)
    TextView mProductContentCount;
    @InjectView(R.id.product_service_person)
    TextView mProductServicePerson;
    @InjectView(R.id.product_used_time)
    TextView mProductUsedTime;
    @InjectView(R.id.pay_confirm)
    TextView mPayConfirm;

    private Context context;
    private ExpenseRecordListResponse.ExpenseBean mExpenseBean;

    public ExpenseRecordCellView(Context context, AttributeSet attrs, int defStyle) {
        this(context, attrs);
    }

    public ExpenseRecordCellView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(context, attrs);
    }

    public ExpenseRecordCellView(Context context) {
        super(context);
        init(context, null);
    }

    private void init(Context context, AttributeSet attrs) {
        this.context = context;
        layoutInflater = LayoutInflater.from(context);
        layoutInflater.inflate(R.layout.widget_expense_record, this);
        ButterKnife.inject(this);
    }

    public void initData(ExpenseRecordListResponse.ExpenseBean expenseBean) {
        if(expenseBean == null){
            return;
        }
        mExpenseBean = expenseBean;
        mProductName.setText(StringUtils.getSpltString(expenseBean.getOrderproductname(), 10));
        mStudentRemarkName.setText("(" + expenseBean.getStudentremarkname() + ")");
        mOrganizationName.setText(expenseBean.getDepartname());
        mRecordTimeInterval.setText(TimeUtils.getDateByTimeMillis(expenseBean.getExpbegindatetime()) + "~" + TimeUtils.getDateByTimeMillis(expenseBean.getExpenddatetime()) );
        mProductContentName.setText(StringUtils.getSpltString(expenseBean.getOrderproductitemname(), 10));
        mProductServicePerson.setText(StringUtils.getSpltString(expenseBean.getProductservicename(),10));
        mProductContentCount.setText(expenseBean.getTimes() + "次");
        mProductUsedTime.setText(TimeUtils.getDateByTimeMillis(expenseBean.getCreatedatetime()));
        if(TextUtils.isEmpty(expenseBean.getStatus()) || expenseBean.getStatus().equals("init")){
            mPayConfirm.setVisibility(View.VISIBLE);
            mPayConfirm.setOnClickListener(this);
        }else{
            mPayConfirm.setVisibility(View.GONE);
        }

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.pay_confirm:
                if(mExpenseRecordCallback != null){
                    mExpenseRecordCallback.onPayConfirm(mExpenseBean.getId());
                }
//                OrderDetailActivity.startActivity(getContext(), mProductDetailBean);
//                ProductDetailActivity.startActivity(context, mProductBean.getProductid());
                break;
            default:
                break;
        }
    }

    public void setExpenseRecordCallback(ExpenseRecordCallback expenseRecordCallback){
        mExpenseRecordCallback = expenseRecordCallback;
    }

    private ExpenseRecordCallback mExpenseRecordCallback;

    public static interface ExpenseRecordCallback{
        void onPayConfirm(String id);
    }


}